//
//  JXLSportSportingViewController.m
//  JXLSport
//
//  Created by 贾晓磊 on 16/10/23.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

#import "JXLSportSportingViewController.h"
#import "JXLSportMapViewController.h"
#import "JXLSportTracking.h"

@interface JXLSportSportingViewController ()

@property (nonatomic,strong) JXLSportMapViewController *mapViewController;

@property (nonatomic,weak) IBOutlet UIButton *mapButton;
@end

@implementation JXLSportSportingViewController

//不要设置控件的位置，因为自动布局没有完成
- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupMapViewController];
}

// 所有子控件布局完成，适合进一步设置其他控件的位置
-(void)viewDidLayoutSubviews {
    //设置罗盘位置
    CGFloat x = _mapButton.center.x - _mapViewController.mapView.compassSize.width * 0.5;
    CGFloat y = _mapButton.center.y - _mapViewController.mapView.compassSize.height * 0.5;
    _mapViewController.mapView.compassOrigin = CGPointMake(x, y);
}

- (IBAction)showMapViewControllerAction:(UIButton *)sender {
    // 模态展现
    [self presentViewController:_mapViewController animated:YES completion:nil];

}

/**
 改变运动状态

 @param sender sender 被点击的按钮
 */
- (IBAction)changeSportStateAction:(UIButton *)sender {
    // 修改地图控制器的运动状态
    _mapViewController.sportTracking.sportState = sender.tag;
}

#pragma mark 设置界面
//设置地图视图控制器
- (void)setupMapViewController {
//   // 1. 实例化地图控制器
        //    JXLSportMapViewController *vc = [[JXLSportMapViewController alloc] init];
//    // 2. 添加到当前控制器的子控制器
//    [self addChildViewController:vc];
//    // 3. 将地图控制器的视图，添加到当前视图
//    [self.view addSubview:vc.view];
//    vc.view.frame = self.view.bounds;
//    // 设置运动类型
//    vc.sportTracking = [[JXLSportTracking alloc] initWithSportType:_sportType];
//    // 4. 完成控制器的添加
//    [vc didMoveToParentViewController:self];
    //获取地图控制器
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"JXLSportSporting" bundle:nil];
    _mapViewController = [sb instantiateViewControllerWithIdentifier:@"sportMapViewController"];
    //设置运动轨迹模型
    _mapViewController.sportTracking = [[JXLSportTracking alloc] initWithSportType:_sportType sportState:JXLSportStateContinue];
}
@end
